// pages/list/list.js
const app = getApp().globalData

function createSong(musicData) {
  return {
    id: musicData.songid,
    mid: musicData.songmid,
    singer: musicData.singer.map(s => s.name).join('/'),
    name: musicData.songname,
    album: musicData.albumname,
    duration: musicData.interval,
    image: `https://y.gtimg.cn/music/photo_new/T002R300x300M000${musicData.albummid}.jpg`,
    url: `http://ws.stream.qqmusic.qq.com/${musicData.songid}.m4a?fromtag=46`
  }
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    zIndex: 50,
    title: '',
    image:'',
    songs: [],
  },
  _getTopMusicList: function () {
    let topid=app.topId;
      const url = `https://c.y.qq.com/v8/fcg-bin/fcg_v8_toplist_cp.fcg?g_tk=1928093487&inCharset=utf-8&outCharset=utf-8&notice=0&format=jsonp&topid=${topid}&needNewCode=1&uin=0&tpl=3&page=detail&type=top&platform=h5&jsonpCallback=jp1`
      return new Promise((resolve, reject) => {
        wx.request({
          url: url,
          success: function (res) {
            resolve(res)
          }
        })
      }).then((res)=>{
      var res1 = res.data.replace('jp1(', '')
      var res2 = JSON.parse(res1.substring(0, res1.length - 1))
      this.setData({
        topList: this._normalizeSongs(res2.songlist),
        title: res2.topinfo.ListName,
        image:this._normalizeSongs(res2.songlist)[0].image,
        bgStyle:`background-image:url(${this._normalizeSongs(res2.songlist)[0].image})`,
        songs:this._normalizeSongs(res2.songlist)
      })
      // console.log(res2.songlist,"songlist")
    })
  },
  _normalizeSongs:function(list) {
    let ret = []
    list.forEach((item) => {
      const musicData = item.data
      if (musicData.songid && musicData.albummid) {
        ret.push(createSong(musicData))
      }
    })
    return ret
  },
  setSonglistTop: function () {
    const _this = this
    wx.createSelectorQuery().in(this).select('#bgImage').boundingClientRect((rect) => {
      rect.height
    }).exec((res) => {
      _this.setData({
        top: res[0].height,
        oldTop: res[0].height //记录原始的top值
      })
    })
  },
  scroll: function (e) {
    let top = this.data.oldTop - e.detail.scrollTop > 0 ? this.data.oldTop - e.detail.scrollTop : 0
    const percent = Math.abs(e.detail.scrollTop / this.data.oldTop)
    if (e.detail.scrollTop > 20) {
      this.setData({
        zIndex: 0,
        bgZindex: 0
      })
    } else if (e.detail.scrollTop < 20 && e.detail.scrollTop > 0) {
      this.setData({
        zIndex: 50,
        bgZindex: 0
      })
    } else if (e.detail.scrollTop < 0) {
      this.setData({
        scale: `scale(${1 + percent})`,
        translate: `transform:translate3d(0px,${Math.abs(e.detail.scrollTop)}px,0px)`,
        bgZindex: 1,
        zIndex: 50
      })
      return
    }
    this.setData({
      top: top
    })
  },
  selectItem: function (e) {
    app.currentIndex = e.currentTarget.dataset.index
    app.songlist = this.data.songs
    wx.setStorageSync('songlist', this.data.songs)
    wx.navigateTo({
      url: '/pages/musicPlay/musicPlay'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getTopMusicList();
    wx.setNavigationBarTitle({
      title: options.title
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setSonglistTop()
    // 动态设置歌手头像背景图
    this.setData({
      bgStyle: `background-image:url(${this.data.image})`
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})